package com.gx.smart.building.ui.spare.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ItemSpareBinding;
import com.gx.smart.building.ui.spare.SelectSparePartActivity;
import com.gx.smart.building.ui.spare.SparePartActivity;
import com.gx.smart.building.ui.spare.model.SelectedSpareStocksMessageEvent;
import com.gx.smart.building.ui.spare.model.SparePart;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareStockDto;
import com.mylhyl.circledialog.CircleDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

/**
 * @author: xiaosy
 * 时间: 2022/4/7
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SpareAdapter extends BaseAdapter<SparePart, SpareAdapter.ViewHolder> {
    public SpareAdapter(Context context) {
        super(context);
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_spare;
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {

        ItemSpareBinding binding = ItemSpareBinding.inflate(LayoutInflater.from(mContext),
                (ViewGroup) view, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindData(ViewHolder holder, SparePart item, int position) {
        int type = item.getType();
        switch (type) {
            case 1:
                holder.binding.spareLayout.setVisibility(View.VISIBLE);
                holder.binding.addSpare.setVisibility(View.GONE);
                break;
            case 2:
                holder.binding.spareLayout.setVisibility(View.GONE);
                holder.binding.addSpare.setVisibility(View.VISIBLE);
                break;
        }
        Map<Integer, SpareStockDto> spareStocks = ((SparePartActivity) mContext).spareStocks;
        Map<Integer, Integer> positionList = ((SparePartActivity) mContext).positionList;
        if (spareStocks != null && spareStocks.containsKey(position)) {
            String spareName = spareStocks.get(position).getSpareDto().getSpareName();
            holder.binding.select.setText(spareName);
        } else {
            holder.binding.select.setText("请选择");
        }
        holder.binding.count.setText(String.valueOf(item.getNum()));
        holder.binding.select.setOnClickListener((view) -> {
            String id = ((SparePartActivity) mContext).item.getId();
            Intent intent = new Intent(mContext, SelectSparePartActivity.class);
            intent.putExtra("warehouseId", id);
            intent.putExtra("position", position);
            mContext.startActivity(intent);
            EventBus.getDefault().postSticky(new SelectedSpareStocksMessageEvent(spareStocks, positionList));
        });
        holder.binding.spareLayout.setTag(position);
        holder.binding.spareLayout.setOnLongClickListener(v -> {
            new CircleDialog.Builder()
                    .setTitle("删除")
                    .setText("您确定要删除备件吗？")
                    .setPositive("确定", v1 -> {
                        int index = (Integer) v.getTag();
                        if (spareStocks != null && spareStocks.containsKey(index)) {
                            spareStocks.remove(spareStocks);
                        }
                        remove(index);
                    })
                    .setNegative("取消", null)
                    .show(((AppCompatActivity) mContext).getSupportFragmentManager());
            return true;
        });
        holder.binding.add.setOnClickListener(v -> {
            if (spareStocks == null || !spareStocks.containsKey(position)) {
                ToastUtils.showLong("请选择");
                return;
            }
            int total = spareStocks.get(position).getStockNum();
            int num = item.getNum();
            if (num >= total) {
                ToastUtils.showLong("个数不能大于库存总数");
                return;
            }
            item.setNum(num + 1);
            holder.binding.count.setText(String.valueOf(item.getNum()));
        });
        holder.binding.reduce.setOnClickListener(v -> {
            if (spareStocks == null || !spareStocks.containsKey(position)) {
                ToastUtils.showLong("请选择");
                return;
            }
            int num = item.getNum();
            if (num <= 1) {
                ToastUtils.showLong("个数小于1");
                return;
            }
            item.setNum(num - 1);
            holder.binding.count.setText(String.valueOf(item.getNum()));
        });
        holder.binding.addSpare.setOnClickListener((view) -> {
            addLastBefore(new SparePart(1));
        });
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemSpareBinding binding;

        public ViewHolder(ItemSpareBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
